package com.gupaoedu.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class CuratorDemoMy {
    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString("192.168.255.143:2181,"
                + "192.168.255.141:2181," +
                "192.168.255.142:2181")
                .sessionTimeoutMs(4000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3)).namespace("curator")//sds
                .build();
        curatorFramework.start();
 //       curatorFramework.delete().deletingChildrenIfNeeded().forPath("/wdt/c");
        //原生api必须父节点存在才能继续创建子节点

        //结果: /curator/mic/node1
        //原生api中，必须是逐层创建，也就是父节点必须存在，子节点才能创建
        /*curatorFramework.create().creatingParentsIfNeeded().
                withMode(CreateMode.PERSISTENT).
                forPath("/mic/node1","1".getBytes());*/
        //删除
//        curatorFramework.delete().deletingChildrenIfNeeded().forPath("/mic/node1");

        curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/wdt/c","sss".getBytes());
        Stat stat=new Stat();
        byte[] bytes = curatorFramework.getData().storingStatIn(stat).forPath("/wdt/c");
        System.out.println(new String(bytes));
        System.out.println("第一次"+stat.getVersion());
        stat=    curatorFramework.setData().withVersion(stat.getVersion()).forPath("/wdt/c","ssss".getBytes());
          bytes = curatorFramework.getData().storingStatIn(stat).forPath("/wdt/c");
        System.out.println(new String(bytes));
        System.out.println("第二次"+stat.getVersion());
       // System.out.println("第二次"+stat.get());
        curatorFramework.close();
    }
}
